import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.prototype.$axios = axios
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    list: [],
    searchList:[]
  },
  mutations: {
    shoplist(state, item) {
      state.list = item
      state.searchList = item
    },
    add_store(state, item) {
      //添加
      state.list.push(item)
    },
    to_search(state, item) {
      state.searchList = []
      //查找
      state.list.forEach(ele=>{
        if(ele.name.includes(item)){
          state.searchList.push(ele)
        }
      })
    }
  },
  actions: {
    shoplist(context) {
      axios.get("./shoplist.json")
        .then(res => {
          console.log(res.data)
          context.commit('shoplist', res.data)
        })
    },
    add_store(context, store) {
      //添加数据
      context.commit('add_store', store)
    },
    to_search(context, searchs) {
      //查找店铺
      context.commit('to_search', searchs)
    }
  },
  modules: {
  }
})
